library(haven)
library(readr)
library(data.table)
library(scales)
library(dplyr)
library(lfe)
library(stargazer)
#library(csranks)

cohort <-fread("H:/Zheng_10223/Joint/cohort_2025.csv")

###########

cohort$refugee=ifelse(cohort$ImmigrationCategory=="Refugee",1,0)

model1=lm(Child_Income_IND_30_34_pct ~ MainParent_Income_HH_MainParentAge45_49_pctparent + factor(refugee)* MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort)

stargazer(model1)
################################################################
##################         IGM TABLE          ##################
################################################################

# 0. Log individual log family excl zero
cohort$Log_Child_Income_IND_30_34 <- log(cohort$Child_Income_IND_30_34)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49)
loglog0 <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[Child_Income_IND_30_34>0 & MainParent_Income_HH_MainParentAge45_49>0])

# 1. Log householdchild  log family excl zero
cohort$Log_Child_Income_HH_30_34 <- log(cohort$Child_Income_HH_30_34)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49)
loglog1 <- lm(Log_Child_Income_HH_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[Log_Child_Income_HH_30_34>0 & MainParent_Income_HH_MainParentAge45_49>0])

# 2. Log individual log family (recode zero to 1)
cohort$Child_Income_IND_30_34_recode <- cohort$Child_Income_IND_30_34; cohort$Child_Income_IND_30_34_recode[which(cohort$Child_Income_IND_30_34==0)] <- 1
cohort$MainParent_Income_HH_MainParentAge45_49_recode <- cohort$MainParent_Income_HH_MainParentAge45_49; cohort$MainParent_Income_HH_MainParentAge45_49_recode[which(cohort$MainParent_Income_HH_MainParentAge45_49==0)] <- 1
cohort$Log_Child_Income_IND_30_34 <- log(cohort$Child_Income_IND_30_34_recode)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49_recode)
loglog2 <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort)

# 3. Log individual log family (recode zero to 1000)
cohort$Child_Income_IND_30_34_recode <- cohort$Child_Income_IND_30_34; cohort$Child_Income_IND_30_34_recode[which(cohort$Child_Income_IND_30_34==0)] <- 1000
cohort$MainParent_Income_HH_MainParentAge45_49_recode <- cohort$MainParent_Income_HH_MainParentAge45_49; cohort$MainParent_Income_HH_MainParentAge45_49_recode[which(cohort$MainParent_Income_HH_MainParentAge45_49==0)] <- 1000
cohort$Log_Child_Income_IND_30_34 <- log(cohort$Child_Income_IND_30_34_recode)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49_recode)
loglog3 <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort)


# 4. Individual rank family 45-49 rank (using income pct of parents)
rankrank1 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort)

# 5. Individual rank family 45-49 rank (using income pct of everyone)
rankrank2 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pcthh, data=cohort)

# 6. Individual rank family ages 15-19 income rank (using income pct of parents)
rankrank3 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_15_19_pct, data=cohort)

# 7. Individual rank family ages 10-19 income rank (using income pct of parents)
rankrank4 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_10_19_pct, data=cohort)

# 8. Individual rank top parent hh income 45-49 income rank (using income pct of parents)
rankrank5 <- lm(Child_Income_IND_30_34_pct~TopParent_Income_HH_45_49_pctparent, data=cohort)

# 9. Individual rank main parent hh income 5y after landing (using income pct of parents)
rankrank6 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_PostLanding5_pctparent, data=cohort)

# 10. Individual rank main parent hh income 10y after landing (using income pct of parents)
rankrank7 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_PostLanding10_pctparent, data=cohort)

# 11. Individual earnings rank family 45-49 rank (using income pct of parents)
rankrank8 <- lm(Child_EmpInc_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort)

# 12. Family rank family 45-49 rank (using income pct of parents)
rankrank9 <- lm(Child_Income_HH_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort)


# Table 3 (main igm estimates)
stargazer(loglog0, rankrank1,rankrank3, rankrank5,rankrank7, rankrank8, out="H:/Zheng_10223/ToVet/Table3.txt")
stargazer(loglog0, rankrank1,rankrank3, rankrank5,rankrank7, rankrank8, out="H:/Zheng_10223/ToVet/Table3.tex")


# Appendix Table (other igm estimates)
stargazer( loglog1, loglog2, loglog3, rankrank2,  out="H:/Zheng_10223/ToVet/Table3AppendixB1_part1.txt")
stargazer( loglog1, loglog2, loglog3, rankrank2,  out="H:/Zheng_10223/ToVet/Table3AppendixB1_part1.tex")


# Appendix Table (other igm estimates)
stargazer(  rankrank4,  rankrank6, rankrank9, out="H:/Zheng_10223/ToVet/Table3AppendixB1_part2.txt")

stargazer(  rankrank4,  rankrank6, rankrank9, out="H:/Zheng_10223/ToVet/Table3AppendixB1_part2.tex")



##Individual rank family 45-49 rank (using income pct of parents)
refugeesind <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othersind <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort[cohort$ImmigrationCategory!="Refugee",])

##Household rank rank family 45-49 rank (using income pct of parents)
refugeeshh <- lm(Child_Income_HH_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othershh <- lm(Child_Income_HH_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort[cohort$ImmigrationCategory!="Refugee",])


## 5 year landing 
##Individual rank family 45-49 rank (using income pct of parents)
refugees5yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_PostLanding5_pctparent, data=cohort[cohort$ImmigrationCategory=="Refugee",])
others5yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_PostLanding5_pctparent, data=cohort[cohort$ImmigrationCategory!="Refugee",])

#10yr 
refugees10yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_PostLanding10_pctparent, data=cohort[cohort$ImmigrationCategory=="Refugee",])
others10yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_PostLanding10_pctparent, data=cohort[cohort$ImmigrationCategory!="Refugee",])


## age of child 

refugeesage10yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_10_19_pct, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othersage10yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_10_19_pct, data=cohort[cohort$ImmigrationCategory!="Refugee",])

stargazer(refugeesage10yr, othersage10yr, 
          out="H:/Zheng_10223/ToVet/Table3bygroupC.txt")
stargazer(refugeesage10yr, othersage10yr, 
          out="H:/Zheng_10223/ToVet/Table3bygroupC.tex")
# Part B
stargazer (refugees5yr, others5yr, refugees10yr,others10yr, add.lines =list( c("Sample","Refugees","Non-Refugees","Refugees","Non-Refugees"),
                                                                             c("Child Income","Ind","Ind","Ind","Ind"),c("Parent Income", "Land Post 5 yr", "Land Post 5 yr", "Land Post 10 yr", "Land Post 10 yr")), 
           out="H:/Zheng_10223/ToVet/Table3bygroupB.tex")


stargazer (refugees5yr, others5yr, refugees10yr,others10yr, add.lines =list( c("Sample","Refugees","Non-Refugees","Refugees","Non-Refugees"),
                                                                             c("Child Income","Ind","Ind","Ind","Ind"),c("Parent Income", "Land Post 5 yr", "Land Post 5 yr", "Land Post 10 yr", "Land Post 10 yr")), 
           out="H:/Zheng_10223/ToVet/Table3bygroupB.txt")


cohort <- fread("H:/Zheng_10223/Joint/cohort_2025.csv")



################################################################
##################         IGM TABLE          ##################
################################################################

# 0. Log individual log family excl zero
cohort$Log_Child_Income_IND_30_34 <- log(cohort$Child_Income_IND_30_34)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49)
loglog0refugee <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[Child_Income_IND_30_34>0 & MainParent_Income_HH_MainParentAge45_49>0 & cohort$ImmigrationCategory=="Refugee",])
loglog0others <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[Child_Income_IND_30_34>0 & MainParent_Income_HH_MainParentAge45_49>0 & cohort$ImmigrationCategory!="Refugee",])

# 1. Log householdchild  log family excl zero
cohort$Log_Child_Income_HH_30_34 <- log(cohort$Child_Income_HH_30_34)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49)
loglog1refugee <- lm(Log_Child_Income_HH_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[Log_Child_Income_HH_30_34>0 & MainParent_Income_HH_MainParentAge45_49>0 & cohort$ImmigrationCategory=="Refugee",])
loglog1others <- lm(Log_Child_Income_HH_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[Log_Child_Income_HH_30_34>0 & MainParent_Income_HH_MainParentAge45_49>0 & cohort$ImmigrationCategory!="Refugee",])

# 2. Log individual log family (recode zero to 1)
cohort$Child_Income_IND_30_34_recode <- cohort$Child_Income_IND_30_34; cohort$Child_Income_IND_30_34_recode[which(cohort$Child_Income_IND_30_34==0)] <- 1
cohort$MainParent_Income_HH_MainParentAge45_49_recode <- cohort$MainParent_Income_HH_MainParentAge45_49; cohort$MainParent_Income_HH_MainParentAge45_49_recode[which(cohort$MainParent_Income_HH_MainParentAge45_49==0)] <- 1
cohort$Log_Child_Income_IND_30_34 <- log(cohort$Child_Income_IND_30_34_recode)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49_recode)

loglog2refugee <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[cohort$ImmigrationCategory=="Refugee",])
loglog2others <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[cohort$ImmigrationCategory!="Refugee",])

# 3. Log individual log family (recode zero to 1000)
cohort$Child_Income_IND_30_34_recode <- cohort$Child_Income_IND_30_34; cohort$Child_Income_IND_30_34_recode[which(cohort$Child_Income_IND_30_34==0)] <- 1000
cohort$MainParent_Income_HH_MainParentAge45_49_recode <- cohort$MainParent_Income_HH_MainParentAge45_49; cohort$MainParent_Income_HH_MainParentAge45_49_recode[which(cohort$MainParent_Income_HH_MainParentAge45_49==0)] <- 1000
cohort$Log_Child_Income_IND_30_34 <- log(cohort$Child_Income_IND_30_34_recode)
cohort$Log_MainParent_Income_HH_MainParentAge45_49 <- log(cohort$MainParent_Income_HH_MainParentAge45_49_recode)
loglog3refugee <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[cohort$ImmigrationCategory=="Refugee",])
loglog3others <- lm(Log_Child_Income_IND_30_34~Log_MainParent_Income_HH_MainParentAge45_49, data=cohort[cohort$ImmigrationCategory!="Refugee",])

stargazer (loglog0refugee, loglog0others, loglog1refugee,loglog1others, add.lines=list( c("Sample","Refugees","Non-Refugees","Refugees","Non-Refugees"),
                                                                                        c("Child Income","Ind","Ind","HH","HH")
                                                                                       ),
 
           out="H:/Zheng_10223/ToVet/Table3bygroupD.tex")


stargazer (loglog2refugee, loglog2others, loglog3refugee,loglog3others, add.lines=list( c("Sample","Refugees","Non-Refugees","Refugees","Non-Refugees"),
                                                                                        c("Child Income","Ind","Ind","Ind","Ind"),
                                                                                        c("Log","Recode 0 to 1","Recode 0 to 1","Recode 0 to 1000","Recode 0 to 1000")
),

out="H:/Zheng_10223/ToVet/Table3bygroupE.tex")

stargazer (loglog0refugee, loglog0others, loglog1refugee,loglog1others, add.lines=list( c("Sample","Refugees","Non-Refugees","Refugees","Non-Refugees"),
                                                                                        c("Child Income","Ind","Ind","HH","HH")
),

out="H:/Zheng_10223/ToVet/Table3bygroupD.txt")


stargazer (loglog2refugee, loglog2others, loglog3refugee,loglog3others, add.lines=list( c("Sample","Refugees","Non-Refugees","Refugees","Non-Refugees"),
                                                                                        c("Child Income","Ind","Ind","Ind","Ind"),
                                                                                        c("Log","Recode 0 to 1","Recode 0 to 1","Recode 0 to 1000","Recode 0 to 1000")
),

out="H:/Zheng_10223/ToVet/Table3bygroupE.txt")

# other tests: 

refugeesage15yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_15_19_pct, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othersage15yr <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_15_19_pct, data=cohort[cohort$ImmigrationCategory!="Refugee",])


refugeesrankrank2 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pcthh, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othersrankrank2 <- lm(Child_Income_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pcthh, data=cohort[cohort$ImmigrationCategory!="Refugee",])




refugeesrankrank5 <- lm(Child_Income_IND_30_34_pct~TopParent_Income_HH_45_49_pctparent, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othersrankrank5 <- lm(Child_Income_IND_30_34_pct~TopParent_Income_HH_45_49_pctparent, data=cohort[cohort$ImmigrationCategory!="Refugee",])


refugeesrankrank8 <- lm(Child_EmpInc_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort[cohort$ImmigrationCategory=="Refugee",])
othersrankrank8 <- lm(Child_EmpInc_IND_30_34_pct~MainParent_Income_HH_MainParentAge45_49_pctparent, data=cohort[cohort$ImmigrationCategory!="Refugee",])

stargazer(refugeesage15yr, othersage15yr, refugeesrankrank2,othersrankrank2, add.lines=list(c("specification","child age 15-19","child age 15-19","rank out of everyone","rank out of everyone"),
                                                                                            c("Sample", "Refugees","Non-refugees","Refugees","Non-Refugees")),
          out="H:/Zheng_10223/ToVet/Table3bygroupF.tex")

stargazer(refugeesage15yr, othersage15yr, refugeesrankrank2,othersrankrank2, add.lines=list(c("specification","child age 15-19","child age 15-19","rank out of everyone","rank out of everyone"),
                                                                                            c("Sample", "Refugees","Non-refugees","Refugees","Non-Refugees")),
          out="H:/Zheng_10223/ToVet/Table3bygroupF.txt")

stargazer(refugeesrankrank5, othersrankrank5, refugeesrankrank8,othersrankrank8, add.lines=list(c("specification","top parent 45-49","top parent 45-49","child earnings","child earnings"),
                                                                                                c("Sample", "Refugees","Non-refugees","Refugees","Non-Refugees")),
          out="H:/Zheng_10223/ToVet/Table3bygroupG.tex")




stargazer(refugeesrankrank5, othersrankrank5, refugeesrankrank8,othersrankrank8, add.lines=list(c("specification","top parent 45-49","top parent 45-49","child earnings","child earnings"),
                                                                                                c("Sample", "Refugees","Non-refugees","Refugees","Non-Refugees")),
          out="H:/Zheng_10223/ToVet/Table3bygroupG.txt")


# D